//
//  HomeViewController.m
//  土巴兔
//
//  Created by 千锋 on 15/12/28.
//  Copyright (c) 2015年 IOS1503薛凯. All rights reserved.
//

#import "HomeViewController.h"
#import "AFNetworking.h"
#import "ADTableViewCell.h"
#import "MenuTableViewCell.h"
#import "LikeTableViewCell.h"
#import "ADModel.h"
#import "Default.h"
#import "MenuFirstViewController.h"
#import "MenuSecondViewController.h"
#import "MenuThirdViewController.h"
#import "MenuFouthViewController.h"
#import "MenuFifthViewController.h"
#import "MenuSixthViewController.h"
#import "MenuSeventhViewController.h"
#import "MenuEighthViewController.h"
#import "MenuNinthViewController.h"
#import "MenuTenthViewController.h"
#import "MineModel.h"
#import "DiaryTableViewCell.h"

#import "TopicTableViewCell.h"
#import "TaoTuTableViewCell.h"
@interface HomeViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (strong,nonatomic)UITableView* tableview;
@property (strong,nonatomic)NSMutableArray* ADDataArray;
@property (strong,nonatomic)NSMutableArray* MenuDataArray;
@property (strong,nonatomic)NSMutableArray* MineDataArray;
@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self relayoutNav];
    _ADDataArray=[[NSMutableArray alloc] initWithCapacity:0];
    _MenuDataArray=[[NSMutableArray alloc] initWithCapacity:0];
    _MineDataArray=[[NSMutableArray alloc] initWithCapacity:0];
    
    [self getDataFromWeb];
    [self getMoreData];
    [self createTableView];
}
#pragma mark- 设置导航
- (void)relayoutNav{
    self.navigationController.navigationBarHidden=YES;
}
- (void)createTableView{

    _tableview=[[UITableView alloc] initWithFrame:self.view.bounds];
    _tableview.delegate=self;
    _tableview.dataSource=self;
    [self.view addSubview:_tableview];
    [_tableview registerClass:[ADTableViewCell class] forCellReuseIdentifier:@"ADTableViewCell"];
    [_tableview registerClass:[MenuTableViewCell class] forCellReuseIdentifier:@"MenuTableViewCell"];
     [_tableview registerClass:[LikeTableViewCell class] forCellReuseIdentifier:@"LikeTableViewCell"];
  
    [_tableview registerClass:[DiaryTableViewCell class] forCellReuseIdentifier:@"DiaryTableViewCell"];
    [_tableview registerClass:[TaoTuTableViewCell class] forCellReuseIdentifier:@"TaoTuTableViewCell"];
    [_tableview registerClass:[TopicTableViewCell class] forCellReuseIdentifier:@"TopicTableViewCell"];
}
#pragma mark- 表格协议
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return _MineDataArray.count+3;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row==0) {
        ADTableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:@"ADTableViewCell"];
        NSMutableArray* arr=[[NSMutableArray alloc] initWithCapacity:0];
        for (ADModel* item in _ADDataArray) {
            [arr addObject:item.imgurl];
        }
        [cell relayoutWithImages:arr];
        return cell;
    }else if (indexPath.row==1){
        MenuTableViewCell* cell2=[tableView dequeueReusableCellWithIdentifier:@"MenuTableViewCell"];
        cell2.delegate=self;
        return cell2;
    }else if (indexPath.row==2){
        LikeTableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:@"LikeTableViewCell"];
        return cell;
    }else{
        if (((MineModel*)_MineDataArray[indexPath.row-3]).type==3) {
            DiaryTableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:@"DiaryTableViewCell"];
            [cell relayoutWithModel:_MineDataArray[indexPath.row-3]];
            return cell;
        }else if (((MineModel*)_MineDataArray[indexPath.row-3]).type==5){
            DiaryTableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:@"DiaryTableViewCell"];
            [cell relayoutWithModel:_MineDataArray[indexPath.row-3]];
            return cell;
        }else if (((MineModel*)_MineDataArray[indexPath.row-3]).type==6){
            TopicTableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:@"TopicTableViewCell"];
            [cell relayoutWithModel:_MineDataArray[indexPath.row-3]];
            return cell;
        }else{
            TaoTuTableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:@"TaoTuTableViewCell"];
            [cell relayoutWithModel:_MineDataArray[indexPath.row-3]];
            return cell;
        }
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row==0) {
        return DEVICEHEIGHT/3;
    }else if (indexPath.row==1) {
        return DEVICEHEIGHT/3;
    }else if (indexPath.row==2){
        return 30;
    }else{
        if (((MineModel*)_MineDataArray[indexPath.row-3]).type==3) {
            return 95;
        }else if (((MineModel*)_MineDataArray[indexPath.row-3]).type==5){
            return 95;
        }else if (((MineModel*)_MineDataArray[indexPath.row-3]).type==6){
            return (DEVICEWIDTH-20)*1/2+95;
        }else{
            return (DEVICEWIDTH-20)*1/2+45;
        }
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
}
#pragma mark- 获取数据
- (void)getDataFromWeb{
    AFHTTPRequestOperationManager* manager=[AFHTTPRequestOperationManager manager];
    NSMutableDictionary* paras=[NSMutableDictionary dictionaryWithDictionary:@{@"action":@"getv_3_6",@"addtype":@"2",@"appid":@"15",@"appname":@"%E5%9C%9F%E5%B7%B4%E5%85%94",@"appostype":@"2",@"appversion":@"3.6.0",@"channel":@"appstore",@"cityName":@"%E8%A5%BF%E5%AE%89",@"idfa":@"F6CB7FAB-6B83-4B41-8A64-5D5A5D781DF7",@"imagesize":@"2",@"module":@"ad",@"systemversion":@"8.3",@"t8t_device_id":@"EE189FEB-9A57-4BF5-9E7F-7C6DFFB15A19",@"to8to_token":@"5577450_38d989893cdbb79b2ea4208102984a5b",@"uid":@"5579680",@"version":@"2.5"}];
    [paras setObject:[[NSUserDefaults standardUserDefaults] objectForKey:@"to8to_token"] forKey:@"to8to_token"];
    [paras setObject:[[NSUserDefaults standardUserDefaults] objectForKey:@"uid"] forKey:@"uid"];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    [manager GET:@"http://mobileapi.to8to.com/index.php" parameters:paras success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary* dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSDictionary* dic2=[dic objectForKey:@"data"];
        NSArray* array1=[dic2 objectForKey:@"bannerInfo"];
        for (int i=0;i<array1.count;i++) {
            NSLog(@"%@",array1[i]);
//            if ([array1[i] ]) {
//                <#statements#>
//            }
            ADModel* model=[[ADModel alloc] initWithDic:array1[i]];
            [_ADDataArray addObject:model];
            [_tableview reloadData];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
}
- (void)getMoreData{
    _MineDataArray=[[NSMutableArray alloc] initWithCapacity:0];
    NSString* urlStr=@"http://mobileapi.to8to.com/index.php";
    NSMutableDictionary* parasDic=[NSMutableDictionary dictionaryWithDictionary:@{@"progress":@"8",@"action":@"broadcast",@"city":@"%E8%A5%BF%E5%AE%89",@"style":@"%E6%97%A5%E5%BC%8F",@"appid":@"15",@"ctime":@"1452243254",@"to8to_token":@"5577450_38d989893cdbb79b2ea4208102984a5b",@"t8t_device_id":@"569eb1056c38e1e8",@"systemversion":@"22",@"imei":@"569eb1056c38e1e8",@"appversion":@"3.6.0",@"appostype":@"1",@"version":@"2.5",@"house":@"%E5%88%AB%E5%A2%85",@"uid":@"5577450",@"h_time":@"1452243254",@"channel":@"%E9%AD%85%E6%97%8F%E5%BA%94%E7%94%A8%E4%B8%AD%E5%BF%83",@"model":@"push"}];
    [parasDic setObject:[[NSUserDefaults standardUserDefaults] objectForKey:@"to8to_token"] forKey:@"to8to_token"];
    [parasDic setObject:[[NSUserDefaults standardUserDefaults] objectForKey:@"uid"] forKey:@"uid"];
    AFHTTPRequestOperationManager* manager=[AFHTTPRequestOperationManager manager];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    [manager POST:urlStr parameters:parasDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary* dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSArray* array=[dic objectForKey:@"data"];
        for (NSDictionary* item in array) {
            MineModel* model=[[MineModel alloc] initWithDic:item];
            [_MineDataArray addObject:model];
            [_tableview reloadData];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}
#pragma mark- 功能cell的点击相应
- (void)pressMenuCellButton:(UIButton*)button{
    switch (button.tag) {
        case 1001:{
            MenuFirstViewController* MenufirstView=[[MenuFirstViewController alloc] init];
            [self.navigationController pushViewController:MenufirstView animated:YES];
            break;
        }
        case 1002:{
            MenuSecondViewController* MenusecondView=[[MenuSecondViewController alloc] init];
            [self.navigationController pushViewController:MenusecondView animated:YES];
            break;
        }
        case 1003:{
            MenuThirdViewController* MenuthirdView=[[MenuThirdViewController alloc] init];
            [self.navigationController pushViewController:MenuthirdView animated:YES];
            break;
        }
        case 1004:{
            MenuFouthViewController* MenufouthView=[[MenuFouthViewController alloc] init];
            [self.navigationController pushViewController:MenufouthView animated:YES];
            break;
        }
        case 1005:{
            MenuFifthViewController* MenufifthView=[[MenuFifthViewController alloc] init];
            [self.navigationController pushViewController:MenufifthView animated:YES];
            break;
        }
        case 1006:{
            MenuSixthViewController* MenusixView=[[MenuSixthViewController alloc] init];
            [self.navigationController pushViewController:MenusixView animated:YES];
            break;
        }
        case 1007:{
            MenuSeventhViewController* MenusevenView=[[MenuSeventhViewController alloc] init];
            [self.navigationController pushViewController:MenusevenView animated:YES];
            break;
        }
        case 1008:{
            MenuEighthViewController* MenueightView=[[MenuEighthViewController alloc] init];
            [self.navigationController pushViewController:MenueightView animated:YES];
            break;
        }
        case 1009:{
            MenuNinthViewController* MenunineView=[[MenuNinthViewController alloc] init];
            [self.navigationController pushViewController:MenunineView animated:YES];
            break;
        }
        case 1010:{
            MenuTenthViewController* MenutenView=[[MenuTenthViewController alloc] init];
            [self.navigationController pushViewController:MenutenView animated:YES];
            break;
        }
        default:
            break;
    }
}
@end
